<?php
class JobWorker
{
    public static function downloadAlbumProfile($job)
    {
        // params
        $params = unserialize($job->workload());
        $userId = $params['userId'];
        $fbToken = $params['fbToken'];

        // facebook
        FacebookUtility::setAccessToken($fbToken);
        $photos = FacebookUtility::getAlbumProfile($userId);

        // download
        foreach($photos as $photo)
        {
            PhotoUtility::addByUrl($userId, $photo['src_big']);
        }
    }

    public static function updateFbData($job)
    {
        // params
        $params = unserialize($job->workload());
        $userId = $params['userId'];
        $fbToken = $params['fbToken'];
        
        // facebook
        FacebookUtility::setAccessToken($fbToken);
        $friends = FacebookUtility::getFriends($userId);
        $userInfo = FacebookUtility::getUserInfo($userId);
        $works = $userInfo['work'];
        $educations = $userInfo['education'];
        
        // update friend
        FriendUtility::deleteByUserId($userId);
        FriendUtility::addFriends($userId, $friends);
        
        // update work
        UserUtility::deleteWork($userId);
        if(is_array($works))
        {
            foreach($works as $work)
            {
                UserUtility::addWork($userId, $work['employer']['id'], $work['employer']['name']);
            }
        }
        
        // update education
        UserUtility::deleteEductaion($userId);
        if(is_array($educations))
        {
            foreach($educations as $education)
            {
                UserUtility::addEducation($userId, $education['school']['id'], $education['school']['name']);
            }
        }
    }
}